/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hangmangame;

import java.util.ArrayList;

/**
 *
 * @author Joana
 */
public class HangManGame {

    protected String word;
    protected String[] dictionary;
    protected ArrayList<Character> wordCharacters;
    protected ArrayList<Character> guessedCharacters;
    protected int trys;
    protected String lastWord;

    public HangManGame() {
        lastWord = "";
        dictionary = HangManDictionary.dictionary;
    }

    public void newGame() {
        word = getWordFromDictionary();
        wordCharacters = new ArrayList<>();
        guessedCharacters = new ArrayList<>();
        trys = 10;
        this.setWordCharacters();
    }

    public String getWordFromDictionary() {
        int i = 1;
        int rand_num = i + (int) (Math.random() * ((HangManDictionary.dictionarySize - i) + 1));
        for (i = 0; i < HangManDictionary.dictionarySize; i++) {
            if (i == rand_num) {
                word = dictionary[i];
                break;
            }
        }
        if(lastWord.equals(word)){
            this.getWordFromDictionary();
        }else{
            lastWord = word;
        }
        return word;
    }

    public void setWordCharacters() {
        wordCharacters = new ArrayList<>();
        if (!word.isEmpty()) {
            for (int i = 0; i < word.length(); i++) {
                if (!checkCharacter(wordCharacters, word.charAt(i))) {
                    wordCharacters.add(word.charAt(i));
                }
            }
        }
    }

    public boolean checkCharacter(ArrayList<Character> charlist, char caracter) {
        boolean found = false;
        int i = 0;
        while (!found && i < charlist.size()) {
            if (Character.toLowerCase(charlist.get(i)) == Character.toLowerCase(caracter)) {
                found = true;
            }
            i++;
        }
        return found;
    }

    public boolean isWin() {
        return wordCharacters.size() == guessedCharacters.size();
    }

    public boolean isLost() {
        return trys < 1;
    }

    public int isEnd() {
        if (isWin()) {
            return 1;
        } else if (isLost()) {
            return 2;
        }
        return 0;
    }

    public boolean addGuess(char character) {
        if (this.checkCharacter(this.wordCharacters, character)) {
            if (!checkCharacter(this.guessedCharacters, character)) {
                this.guessedCharacters.add(character);
            }
            return true;
        } else {
            trys--;
            return false;
        }
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public int getTrys() {
        return trys;
    }

    public ArrayList<Character> getGuessedCharacters() {
        return guessedCharacters;
    }

    public void setGuessedCharacters(ArrayList<Character> guessedCharacters) {
        this.guessedCharacters = guessedCharacters;
    }
    
}
